package com.tacc.tyun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tacc.tyun.base.BaseServiceImpl;
import com.tacc.tyun.dao.master.MsgBannerDao;
import com.tacc.tyun.model.MsgBanner;
import com.tacc.tyun.service.MsgBannerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
*
* @author congcong tao
* @date 2017/8/12 9:55
*/
@Service("msgBannerService")
public class MsgBannerServiceImpl extends BaseServiceImpl<MsgBanner> implements MsgBannerService {

    @Resource
    private MsgBannerDao msgBannerDao;

    @Autowired
    public void setBaseMapper(MsgBannerDao msgBannerDao) {
        this.baseDao = msgBannerDao;
    }


    @Override
    public PageInfo<MsgBanner> pageInfo(MsgBanner msgBanner, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(MsgBanner.class);
        Example.Criteria criteria=example.createCriteria();
        if (StringUtils.isNotEmpty(msgBanner.getImageTitle())){
            criteria.andLike("imageTitle","%"+msgBanner.getImageTitle()+"%");
        }
        example.setOrderByClause("status asc,sort asc,updateTime desc");//asc升序desc降序
        criteria.andEqualTo("removeTag",msgBanner.getRemoveTag());
        //需要关联查询，多表
        List<MsgBanner> list=msgBannerDao.selectByExample(example);
        return new PageInfo<MsgBanner>(list);
    }

    @Override
    public int deleteBannerByIds(String[] ids, Integer removeTag) {
        return msgBannerDao.deleteBannerByIds(ids,removeTag);
    }

    @Override
    public int updateStatusByIds(List<String> ids, String status) {
        return msgBannerDao.updateStatusByIds(ids,status);
    }

    @Override
    public int updateSortEntity(String[] ids) {
        int i=0;
        if (ids!=null && ids.length>0){
            //转化为map
            i=1;
            for(String id:ids){
                if (StringUtils.isNotEmpty(id)){
                    MsgBanner record=new MsgBanner();
                    record.setBannerId(id);
                    record.setSort(String.valueOf(i));
                    msgBannerDao.updateByPrimaryKeySelective(record);
                }else{
                    i=0;
                    break;
                }
                i++;
            }
        }
        //todo 将其他排序除掉变为Null

        return i;
    }

}
